import { LocalStorage } from '@/api/LocalStorage'
import Vue from 'vue'
import VueI18n, { DateTimeFormats, LocaleMessages } from 'vue-i18n'
import enLocale from './en-US'
import zhLocale from './zh-CN'


Vue.use(VueI18n)

const messages: LocaleMessages = {
    "en-US": enLocale,
    "zh-CN": zhLocale
};

export const getLocale = () => {
    const storeLan = LocalStorage.getLanguage();
    if (storeLan) {
        return storeLan;
    }
    const language = navigator.language.toLowerCase()
    const locales = Object.keys(messages)
    for (const locale of locales) {
        if (language.indexOf(locale) > -1) {
            // console.log(locale)
            return locale
        }
    }
    return 'en'
}


const dateTimeFormats: DateTimeFormats = {
    'en': {
        short: {
            year: 'numeric', month: 'short', day: 'numeric'
        },
        long: {
            year: 'numeric', month: 'numeric', day: 'numeric',
            hour: 'numeric', minute: 'numeric', hour12: false
        }
    },
    'zh': {
        short: {
            year: 'numeric', month: 'short', day: 'numeric'
        },
        long: {
            year: 'numeric', month: 'numeric', day: 'numeric',
            hour: 'numeric', minute: 'numeric', second: "numeric", hour12: false
        }
    }
}

const i18n = new VueI18n({
    locale: getLocale(),
    messages,
    dateTimeFormats,
    fallbackLocale: ['zh-CN', "en-US"],
    silentTranslationWarn: true
});
export default i18n;